﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.Supervise.Model;
using HZ.Supervise.IBLL;
using System.Collections.Generic;
using HZ.Supervise.Model.Enum;
using HZ.Supervise.DAL;

namespace HZ.Supervise.Web.TotalDrugs
{
    public partial class ProcureCatalogList : BasePage
    {

        public ProcureCatalogList()
        {
            this.ModuleNo = Module.药品统计分析.ToString();
            this.PageOperateType = OperateType.所有中标品规.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                setBind();
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            setBind();
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            setBind();
        }

        /// <summary>
        /// 根据查询条件绑定采购目录
        /// </summary>
        private void setBind()
        {
            //获取查询条件
            Model.Query.ProcureCatalog procureCatalog = new HZ.Supervise.Model.Query.ProcureCatalog();
            procureCatalog.GoodsID = DAL_Helper.Replace(txtGoodsId.Text.Trim());
            procureCatalog.CompanyName_SC = DAL_Helper.Replace(txtProduceCompany_SC.Text.Trim());
            procureCatalog.MedicineModelName = DAL_Helper.Replace(txtMedicineModel.Text.Trim());
            procureCatalog.Outlookc = DAL_Helper.Replace(txtOutLook.Text.Trim());
            procureCatalog.Unit = DAL_Helper.Replace(txtUnit.Text.Trim());
            procureCatalog.IsUsing = true;
            procureCatalog.Flag = string.IsNullOrEmpty(ddlPurchaseRange.SelectedItem.Value) ? null : new HZ.Common.ConvertHelper(ddlPurchaseRange.SelectedItem.Value).ToInt32;

            if (HZ.Common.MyValidate.IsNumeric(txtFactor.Text.Trim()))
            {
                procureCatalog.Factor = Convert.ToInt32(txtFactor.Text.Trim());
            }
            if (!string.IsNullOrEmpty(ddlSortSource.SelectedItem.Value))
            {
                procureCatalog.SortSource = new HZ.Common.ConvertHelper(ddlSortSource.SelectedItem.Value).ToInt32;
            }
            procureCatalog.CompanyName_TB = DAL_Helper.Replace(txtCompany_TB.Text);
            //procureCatalog.SortName = txtProductType.Text;
            procureCatalog.GoodsName = DAL_Helper.Replace(txtGoodsName.Text.Trim());
            procureCatalog.Append = string.Format("ProductName like '%{0}%' or PinyinCode like '%{0}%'", DAL_Helper.Replace(txtProductName.Text.Trim()));
            procureCatalog.SortFields = " ProductName asc";


            //调用方法，执行查询，分页显示药品信息
            //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
            int totalCount = 0;
            IBLL_ProcureCatalog ipb =BLLFactory.IBLL_ProcureCatalog();
            try
            {

                IList<Model.View.ProcureCatalog> procureCatalogList = ipb.GetProcureCatalogByWhere(procureCatalog, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
                AspNetPager1.RecordCount = totalCount;
                gvwProduceCatalog.DataSource = procureCatalogList;
                gvwProduceCatalog.DataBind();
            }
            catch (Exception ex)
            {
                HZ.Web.WebUtil.Alert("获取数据出错！",this);
            }
        }

        /// <summary>
        /// 简单查询与高级查询间的转换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbnSwitch_Click(object sender, EventArgs e)
        {
            if (lbnSwitch.CommandArgument == "simple")
            {
                ltlAdvance.Text = "";
                lbnSwitch.CommandArgument = "advance";
                lbnSwitch.Text = "简单查询";
            }
            else
            {
                ltlAdvance.Text = "<tbody id='advance' style='display:none'>";
                lbnSwitch.CommandArgument = "simple";
                lbnSwitch.Text = "高级查询";

                //清空数据
                txtCompany_TB.Text = "";
                txtFactor.Text = "";
                txtProduceCompany_SC.Text = "";
                //txtProductType.Text = "";
                txtUnit.Text = "";
                txtGoodsName.Text = "";
                ddlSortSource.SelectedIndex = -1;
                txtOutLook.Text = "";
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            //获取查询条件
            Model.Query.ProcureCatalog procureCatalog = new HZ.Supervise.Model.Query.ProcureCatalog();
            procureCatalog.GoodsID = DAL_Helper.Replace(txtGoodsId.Text.Trim());
            procureCatalog.CompanyName_SC = DAL_Helper.Replace(txtProduceCompany_SC.Text.Trim());
            procureCatalog.MedicineModelName = DAL_Helper.Replace(txtMedicineModel.Text.Trim());
            procureCatalog.Outlookc = DAL_Helper.Replace(txtOutLook.Text.Trim());
            procureCatalog.Unit = DAL_Helper.Replace(txtUnit.Text.Trim());
            procureCatalog.IsUsing = true;
            procureCatalog.Flag = string.IsNullOrEmpty(ddlPurchaseRange.SelectedItem.Value) ? null : new HZ.Common.ConvertHelper(ddlPurchaseRange.SelectedItem.Value).ToInt32;

            if (HZ.Common.MyValidate.IsNumeric(txtFactor.Text.Trim()))
            {
                procureCatalog.Factor = Convert.ToInt32(txtFactor.Text.Trim());
            }
            if (!string.IsNullOrEmpty(ddlSortSource.SelectedItem.Value))
            {
                procureCatalog.SortSource = new HZ.Common.ConvertHelper(ddlSortSource.SelectedItem.Value).ToInt32;
            }
            procureCatalog.CompanyName_TB = DAL_Helper.Replace(txtCompany_TB.Text);
            procureCatalog.GoodsName = DAL_Helper.Replace(txtGoodsName.Text.Trim());
            procureCatalog.Append = string.Format("ProductName like '%{0}%' or PinyinCode like '%{0}%'", DAL_Helper.Replace(txtProductName.Text.Trim()));
            procureCatalog.SortFields = " ProcureCatalogID asc";

            int totalCount = 0;
            IBLL_ProcureCatalog ipb = BLLFactory.IBLL_ProcureCatalog();
            IList<Model.View.ProcureCatalog> procureCatalogList = ipb.GetProcureCatalogByWhere(procureCatalog, 1, 0, out totalCount);

            System.Data.DataTable dt = HZ.Data.ModelConvert.GetDataTableFromModel(procureCatalogList);
            ExportFile.Instance.ExportData(dt, gvwProduceCatalog, this);

        }
    }
}
